
var TasksDefinitions = new Object();

TasksDefinitions.createTableModel = function() {
	
	var cmd = new Object();
	cmd.operation = "datastore.list"
	cmd.type = "taskDefinition";
	
	var result = WindData.execute(cmd);	
	var tableModel = WindTable.createModel();
	tableModel.setTitle("Task Definitions");
	tableModel.clearButtons();
	{
		var button = new WindButtonModel();
		button.setTitle("New");
		tableModel.addButton(button);
		
		button.onClick = function() {
			var dialogView = TasksDefinitions.createDetailDialog("insert");
			dialogView.onClose = function(code) {
				if(code=="inserted") {
					TasksDefinitions.updateTable(tableModel);
				}
			}
			dialogView.open();
		}
	}
	{
		var button = new WindButtonModel();
		button.setTitle("Edit");
		tableModel.addButton(button);
		
		button.onClick = function() {
			var selected = tableModel.getSelectedRows();
			if(selected.length==1) {
				key = selected[0].getCell(0);
				
				var cmd = new Object();
				cmd.operation = "datastore.list"
				cmd.key = key;
				
				var response = WindData.execute(cmd)
				var taskDefinition = response.entity;
				
				var dialogView = TasksDefinitions.createDetailDialog("update", taskDefinition);
				dialogView.onClose = function(code) {
					if(code=="updated") {
						TasksDefinitions.updateTable(tableModel);
					}
				}
				dialogView.open();
			}			
		}
	}
	{
		var button = new WindButtonModel();
		button.setTitle("Delete");
		tableModel.addButton(button);
		
		button.onClick = function() {
			var selected = tableModel.getSelectedRows();
			var keys = "";
			for(var i=0; i<selected.length; i++) {
				keys += selected[i].getCell(0);
				if(i<(selected.length-1)) {
					keys += ",";
				}
			}
			
			var cmd = new Object();
			cmd.operation = "datastore.nop"
			cmd.keys = keys;
			
			var response = WindData.execute(cmd);
			
			if(response.errors.length==0) {
				tableModel = TasksDefinitions.updateTable(tableModel);
				var tableView = tableModel.getView();
				tableView.addInfo("Deleted finished succsesfully")
			} else {
				for(var i=0; i<response.errors.length; i++) {
					tableModel.getView().addError(response.errors[i]);
				}
			}
		}
	}
	
	tableModel.clearColumns();
	{
		var column = new WindTableColumnModel();
		column.setTitle("Key");
		column.setVisible(false);
		tableModel.addColumn(column);
	}
	{
		var column = new WindTableColumnModel();
		column.setTitle("Code");
		tableModel.addColumn(column);
	}

	{
		var column = new WindTableColumnModel();
		column.setTitle("Title");
		tableModel.addColumn(column);
	}

	tableModel.clearRows();
	for(var i=0; i<result.rows.length; i++) {
		var row = new WindTableRowModel();
		var key = result.rows[i].key;
		var code = result.rows[i].code;
		var title = result.rows[i].title;
		row.addCell(key);
		row.addCell(code);
		row.addCell(title);
		tableModel.addRow(row);
	}
	
	return tableModel;
}

TasksDefinitions.createTableView = function(tableModel) {
	if(tableModel==null) {
		var tableModel = TasksDefinitions.createTableModel();
	}
	var tableView = WindTable.createView(tableModel);
	return tableView;
}

TasksDefinitions.updateTable = function(tableModel) {
	var oldView = tableModel.getView();
	var newModel = TasksDefinitions.createTableModel();
	var newView = TasksDefinitions.createTableView(newModel);
	WindWindow.updateView(oldView, newView);
	return newModel;
} 

TasksDefinitions.createDetailDialog = function(mode, taskDefinition) {
	var dialogView = WindDialog.createView();

	if(mode==null) {
		mode = "insert";
	}
	
	if(mode=="insert") {
		dialogView.setTitle("Insert a new task definition");
	} else if(mode=="update") {
		dialogView.setTitle("Update an existing task definition");
	}
	
	if(mode=="insert") {
		var button = WindDialog.createToolbarButtonView();
		button.setText("Insert");
		button.onClick = function() {
			var title = titleText.getValue();
			var code = codeText.getValue();
			var description = descriptionText.getValue();
			var cmd = new Object();
			cmd.operation = "datastore.insert";
			cmd.entity = new Object();
			cmd.entity.type = "taskDefinition";
			cmd.entity.title = title;
			cmd.entity.code = code;
			cmd.entity.description = description;
			var result = WindData.execute(cmd);
			if(result.errors.length==0) {
				dialogView.close("inserted");
			} else {
				dialogView.clearMessages();
				for(var i=0; i<result.errors.length; i++) {
					var error = result.errors[i];
					dialogView.addError(error);
				}
			}
		}
		dialogView.getToolbar().addView(button);
	}
	
	if(mode=="update") {
		var button = WindDialog.createToolbarButtonView();
		button.setText("Update");
		button.onClick = function() {
			var title = titleText.getValue();
			var code = codeText.getValue();
			var description = descriptionText.getValue();
			var cmd = new Object();
			cmd.operation = "datastore.update";
			cmd.entity = new Object();
			cmd.entity.type = "taskDefinition";
			cmd.entity.key = taskDefinition.key;
			cmd.entity.title = title;
			cmd.entity.code = code;
			cmd.entity.description = description;
			var result = WindData.execute(cmd);
			if(result.errors.length==0) {
				dialogView.close("updated");
			} else {
				dialogView.clearMessages();
				for(var i=0; i<result.errors.length; i++) {
					var error = result.errors[i];
					dialogView.addError(error);
				}
			}
		}
		dialogView.getToolbar().addView(button);
	}
	
	{
		var button = WindDialog.createToolbarButtonView();
		button.setText("Cancel");
		button.onClick = function() {
			dialogView.close();
		}
		dialogView.getToolbar().addView(button);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("codeLabel");
		label.setText("Code:"); 
		label.setX(25);
		label.setY(28);
		dialogView.getBody().addView(label);
	}
	
	var codeText = WindText.createView();
	codeText.setId("codeText");
	codeText.setX(65);
	codeText.setY(0);
	if(taskDefinition!=null) {
		codeText.setValue(taskDefinition.code);
	}
	dialogView.getBody().addView(codeText);
	
	{
		var label = WindLabel.createView();
		label.setId("titleLabel");
		label.setText("Title:"); 
		label.setX(25);
		label.setY(28);
		dialogView.getBody().addView(label);
	}
	
	var titleText = WindText.createView();
	titleText.setId("titleText");
	titleText.setX(65);
	titleText.setY(0);
	if(taskDefinition!=null) {
		titleText.setValue(taskDefinition.title);
	}
	dialogView.getBody().addView(titleText);
	
	{
		var label = WindLabel.createView();
		label.setId("descriptionLabel");
		label.setText("Descriptoin:"); 
		label.setX(25);
		label.setY(28);
		dialogView.getBody().addView(label);
	}
	
	var descriptionText = WindText.createView();
	descriptionText.setId("descriptionText");
	descriptionText.setLines(10);
	descriptionText.setX(27);
	descriptionText.setY(23);
	descriptionText.setWidth(439);
	descriptionText.setHeight(134);
	if(taskDefinition!=null) {
		descriptionText.setValue(taskDefinition.description);
	}
	dialogView.getBody().addView(descriptionText);

	
	return dialogView;
}


